﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAO
{
    public class UserService
    {
        /// <summary>
        ///     通过指定用户ID得到该用户的所有资料
        /// </summary>
        /// <param name="Username" type="String">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回用户的所有资料
        /// </returns>
        public DataTable GetUserByName(String Username)
        {
            String CMD = "SELECT * FROM TB_USER WHERE Username='" + Username + "'";
            SqlDataAdapter Adapter = new SqlDataAdapter(CMD, DB.DB_CONN);
            using (DataSet DS = new DataSet("USERINFO"))
            {
                Adapter.Fill(DS, "DT_USERINFO");
                return DS.Tables["DT_USERINFO"];
            }
        }

        /// <summary>
        ///     通过指定用户ID得到该用户的所有资料
        /// </summary>
        /// <param name="Username" type="String">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回用户的数据集
        /// </returns>
        public DataSet DS_GetUserByName(String Username)
        {
            String CMD = "SELECT * FROM TB_USER WHERE Username='" + Username + "'";
            SqlDataAdapter Adapter = new SqlDataAdapter(CMD, DB.DB_CONN);
            using (DataSet DS = new DataSet("USERINFO"))
            {
                Adapter.Fill(DS, "DT_USERINFO");
                return DS;
            }
        }

        /// <summary>
        ///     通过指定用户ID得到该用户的所有资料
        /// </summary>
        /// <param name="UserId" type="int">
        ///     <para>
        ///         用户Id
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回用户的所有资料
        /// </returns>
        public DataTable GetUserByName(int UserId)
        {
            String CMD = "SELECT * FROM TB_USER WHERE UserId=" + UserId;
            SqlDataAdapter Adapter = new SqlDataAdapter(CMD, DB.DB_CONN);
            using (DataSet DS = new DataSet("USERINFO"))
            {
                Adapter.Fill(DS, "DT_USERINFO");
                return DS.Tables["DT_USERINFO"];
            }
        }

        /// <summary>
        ///     修改用户个性签名
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="NewProd" type="string">
        ///     <para>
        ///         新的个性签名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回false , 则表示更新失败
        /// </returns>
        public bool ModifyUserProd(String username, String NewProd)
        {
            using (SqlDataAdapter Adapter = new SqlDataAdapter())
            {
                DB.DB_CONN.Open();
                using (SqlCommand sqlCommand = new SqlCommand("UPDATE TB_USER SET Prod='" + NewProd + "' WHERE Username='" + username + "'", DB.DB_CONN))
                {
                    if (sqlCommand.ExecuteNonQuery() > 0)
                    {
                        DB.CloseDB();
                        return true;
                    }
                }
            }
            DB.CloseDB();
            return false;
        }

        /// <summary>
        ///     更新指定用户字段信息
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="DB_FILED" type="string">
        ///     <para>
        ///         数据库关联字段
        ///     </para>
        /// </param>
        /// <param name="NewValue" type="string">
        ///     <para>
        ///         新值
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回 false, 则表示更新失败
        /// </returns>
        public bool Update(String username, String DB_FILED, String NewValue)
        {
            using (SqlDataAdapter Adapter = new SqlDataAdapter())
            {
                DB.DB_CONN.Open();
                using (SqlCommand sqlCommand = new SqlCommand("UPDATE TB_USER SET " + DB_FILED + "='" + NewValue + "' WHERE Username='" + username + "'", DB.DB_CONN))
                {
                    if (sqlCommand.ExecuteNonQuery() > 0)
                    {
                        DB.CloseDB();
                        return true;
                    }
                }
            }
            DB.CloseDB();
            return false;
        }
    }
}
